<!DOCTYPE html>
<html>

<head>
  <title>User Login</title>
  <script src="../../js/axios.min.js"></script>
  <script src="../../js/jquery.js"></script>
  <script src="../../js/bootstrap.min.js"></script>
  <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" type="text/css" href="../../css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="../../css/login.css">
 
</head>

<body>
  <div class="container">
    <div class="row justify-content-center align-items-center h-100">
      <div class="col-md-6 col-lg-4">
        <div class="card">
          <div class="card-body">
            <h3 class="card-title mb-4 text-center text-primary custom-title">同盾</h3>
            <form id="loginForm">
              <div class="mb-3">
                <label for="account" class="form-label">账号</label>
                <input type="text" class="form-control" id="account" name="account" required>
              </div>
              <div class="mb-3">
                <label for="password" class="form-label">密码</label>
                <input type="password" class="form-control" id="password" name="password" required>
              </div>
              <div class="d-grid gap-2">
                <button type="submit" class="btn btn-primary">登录</button>
              </div>
            </form>
            <div id="message" class="error-message mt-3"></div>
            <div class="text-center mt-3">
              <a href="register.html" class="text-secondary">注册页面</a> |
              <a href="changePassword.html" class="text-secondary">忘记密码</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>



  <script>
    document.getElementById('loginForm').addEventListener('submit', function (event) {
      event.preventDefault(); // 阻止表单默认提交行为

      var account = document.getElementById('account').value;
      var password = document.getElementById('password').value;

      axios.post('http://localhost:8888/api/user/login', {
        account: account,
        password: password
      })
        .then(function (response) {
          if (response.data.code === 0) {
            localStorage.setItem("userId", response.data.data.userId);
            localStorage.setItem("userRole", response.data.data.userRole);
            localStorage.setItem("userName", response.data.data.name);
            var userId = localStorage.getItem('userName');
            localStorage.setItem("account", response.data.data.account);
            // 根据用户权限重定向到不同的主页
            switch (response.data.data.userRole) {
              case 0:
                window.location.href = '../index/userIndex.html'; // 用户主页
                break;
              case 1:
                window.location.href = '../index/adminIndex.html'; // 管理员主页
                break;
              case 2:
                window.location.href = '../index/superAdminIndex.html'; // 超级管理员主页
                break;
              default:
                // 处理未知的用户权限
                break;
            }
          } else {
            document.getElementById('message').textContent = response.data.message;
          }
        })
        .catch(function (error) {
          document.getElementById('message').textContent = 'An error occurred during the login process.';
        });
    });
  </script>
</body>

</html>